package org.example;
import java.io.File;
import java.text.MessageFormat;
import java.util.Objects;

public class MavenInstallCmd {
    private static String CMD="mvn install:install-file -Dfile={0} -DgroupId={1} -DartifactId={2} -Dversion={3} -Dpackaging={4}";
    public static void main(String[] args){
        File directory=new File("E:\\ioc\\maven-lib\\lib\\黄埔");

        // 调用遍历方法
        if (directory.exists() && directory.isDirectory()) {
            traverseDirectory(directory, null,"com.tencent.intelcity","");
        } else {
            System.out.println("目录不存在或不是一个有效的目录.");
        }
    }

    private static void traverseDirectory(File dir, String basePath,String filterPackage,String fileArtifact) {
        // 获取目录下的所有文件和子目录
        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                // 计算相对路径
                String relativePath = basePath==null? file.getName():basePath + File.separator + file.getName();

                if (file.isDirectory()) {
                    File jarFile=containsExtNameFiles(file,".jar");
                    File pomFile=containsExtNameFiles(file,".pom");
                    if(jarFile!=null){
                        String cmdStr=getCmd(jarFile,relativePath,"jar",filterPackage,fileArtifact);
                        if(cmdStr!=null){
                            // 输出文件的相对路径
                            System.out.println(cmdStr);
                        }
                    } else if(pomFile!=null){
                        String cmdStr=getCmd(pomFile,relativePath,"pom",filterPackage,fileArtifact);
                        if(cmdStr!=null){
                            // 输出文件的相对路径
                            System.out.println(cmdStr);
                        }
                    }else{
                        // 递归遍历子目录
                        traverseDirectory(file, relativePath,filterPackage,fileArtifact);
                    }
                }
            }
        }
    }

    private static File containsExtNameFiles(File dir,String extName) {
        // 获取目录下的所有文件和子目录
        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                if(!file.isDirectory()) {
                    String parentName=file.getParentFile().getName();
                    if (file.getName().endsWith(parentName+extName)) {
                        // 如果找到 .jar 文件，返回 true
                        return file;
                    }
                }
            }
        }
        return null; // 如果没有找到 .jar 文件，返回 false
    }

    public static String replaceLast(String original, String toReplace, String replacement) {
        int lastIndex = original.lastIndexOf(toReplace);
        if (lastIndex == -1) {
            return original; // 如果找不到匹配，返回原字符串
        }

        // 构造新的字符串
        String beforeLast = original.substring(0, lastIndex);
        String afterLast = original.substring(lastIndex + toReplace.length());

        return beforeLast + replacement + afterLast;
    }

    private static String getCmd(File file,String relativePath,String extName,String filterPackage,String fileArtifact){
        String jarPath=file.getPath();

        String s= Objects.equals(File.separator, "\\") ?"\\\\":File.separator;
        String[] splitRelativePath=relativePath.split(s);
        String version=splitRelativePath[splitRelativePath.length-1];
        relativePath=relativePath.replace(File.separator+version,"");
        String groupId=relativePath.replace(File.separator,".");

        String artifactId=file.getName().replace("."+extName,"");
        artifactId=artifactId.replace("-"+version,"");

        groupId=replaceLast(groupId,"."+artifactId,"");

        if(groupId.contains(filterPackage) && artifactId.indexOf(fileArtifact)==0){
            return MessageFormat.format(CMD, jarPath,groupId,artifactId,version, extName);
        }else{
            return null;
        }
    }
}
